package com.baizhi.controller;

import com.baizhi.entity.City;
import com.baizhi.service.CityService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("city")
public class CityController {
    @Autowired
    private CityService cityService;

    @RequestMapping("add")
    @ResponseBody
    public Map addCity(City city){
        Map map = new HashMap();
        try {
            cityService.insert(city);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("remove")
    @ResponseBody
    public Map removeCity(Integer id){
        Map map = new HashMap();
        try {
            cityService.delete(id);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("show")
    @ResponseBody
    public Map showCity(String name,Integer page,Integer limit){
        System.out.println("搜索收参name="+name);
        Map map = new HashMap();
        PageInfo<City> pageInfo = cityService.selectByPage(name, page, limit);
        map.put("code",0);
        map.put("msg","");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }
}
